package com.example.surfaceprovider.slice;

import com.example.surfaceprovider.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.OnClickListener;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.Surface;
import ohos.agp.graphics.SurfaceOps;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.global.resource.RawFileDescriptor;
import ohos.media.common.Source;
import ohos.media.common.VideoProperty;
import ohos.media.player.Player;

import java.io.IOException;

public class MainAbilitySlice extends AbilitySlice {
    SurfaceProvider surfaceProvider;
    Image image;
    Player player=new Player(this);
    String url = "https://vd4.bdstatic.com/mda-kfhqtqzx15ym5s1y/hd/mda-kfhqtqzx15ym5s1y.mp4";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        surfaceProvider= (SurfaceProvider) findComponentById(ResourceTable.Id_surfaceprovider);

        image= (Image) findComponentById(ResourceTable.Id_image);
        surfaceProvider.getSurfaceOps().get().addCallback(new VidePlayer());
        surfaceProvider.pinToZTop(false);
        WindowManager.getInstance().getTopWindow().get().setTransparent(true);
        surfaceProvider.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (player.isNowPlaying()){
                    player.pause();
                    image.setVisibility(Component.VISIBLE);
                }else {
                    player.play();
                    image.setVisibility(Component.HIDE);
                }
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private class VideoPlayerCallback implements Player.IPlayerCallback {
        @Override
        public void onPrepared() {

        }

        @Override
        public void onMessage(int i, int i1) {

        }

        @Override
        public void onError(int i, int i1) {

        }

        @Override
        public void onResolutionChanged(int i, int i1) {

        }

        @Override
        public void onPlayBackComplete() {
            if (player != null) {
                player.prepare();
                player.play();
            }
        }

        @Override
        public void onRewindToComplete() {

        }

        @Override
        public void onBufferingChange(int i) {

        }

        @Override
        public void onNewTimedMetaData(Player.MediaTimedMetaData mediaTimedMetaData) {

        }

        @Override
        public void onMediaTimeIncontinuity(Player.MediaTimeInfo mediaTimeInfo) {

        }
    }
    private class VidePlayer implements SurfaceOps.Callback{

        @Override
        public void surfaceCreated(SurfaceOps surfaceOps) {
            if (surfaceProvider.getSurfaceOps().isPresent()) {
                RawFileDescriptor filDescriptor = null;
//                try {
//                    filDescriptor = getResourceManager().getRawFileEntry("entry/resources/base/media/video01.mp4").openRawFileDescriptor();
                    Source source = new Source(url);
                    Surface surface = surfaceProvider.getSurfaceOps().get().getSurface();
                    player.setSource(source);
                    player.setVideoSurface(surface);
                    player.setPlayerCallback(new VideoPlayerCallback());
                    player.prepare();
                    surfaceProvider.setTop(0);
                    player.play();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }

            }
        }

        @Override
        public void surfaceChanged(SurfaceOps surfaceOps, int i, int i1, int i2) {

        }

        @Override
        public void surfaceDestroyed(SurfaceOps surfaceOps) {

        }
    }
}
